The VBA TRIM Function returns a string after removing spaces from the left and right of the string.
Trim(text)
Remove extraneous spaces from the beginning and end of the string:
Sub TrimExample1()
MsgBox Trim("test") 'Returns: "test"
MsgBox Trim(" test") 'Returns: "test"
MsgBox Trim("test ") 'Returns: "test"
MsgBox Trim(" test ") 'Returns: "test"
MsgBox Trim(" test test ") 'Returns: "test test"
MsgBox Trim(" ") 'Returns: ""
End Sub
Remove extraneous spaces from the inside of the string:
Sub TrimExample2()
MsgBox WorksheetFunction.TRIM(" test test ") 'Returns: "test test"
End Sub